package controllers
/*********************************************************/
/***************本文档中的程序用于普通用户的操作相关***********/
/*********************version:1.0*************************/
/****Copyright (c) 2014年 许剑华. All rights reserved******/
/*********************************************************/

///Users/xujianhua/Documents/Test/
import (
	//"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
	"fmt"
	"log"
	//"encoding/json"

	//"os"
	//"io"
	"time"
	//"strings"
    //"reflect"
	//"smart_home/models"
	//"github.com/astaxie/beego/orm"
	m "smart_home/models"
	."smart_home/lib"
	//"strconv"
	//"github.com/beego/samples/WebIM/models"
	"strconv"
	//"math"
)
type RespSynchroData struct {
	RespCode			int
	Data				[]m.FileManager
}
type UserGroup struct {
	beego.Controller
}

type LastVersionType struct {
	RespCode  int
	Data 	  m.NewVersionApp
}
//var data_url string = beego.AppConfig.String("data_url")
var  data_file_name string = beego.AppConfig.String("data_file_name")
var  down_app_url  string = "http://devices.coffee-time.cn:8080/api/download_app/"
func (this *UserGroup)AddUserSesssion(userid string,Encryptionkey string){
		mUserSessionType := UserSessionType{LastDate:time.Now(),Count:0,IP:this.Ctx.Input.IP(),Encryptionkey:Encryptionkey}
		this.SetSession(userid, mUserSessionType)
		fmt.Println("this.GetSession(userid) ",this.GetSession(userid))
}
func (this *UserGroup)CheckSession(userid string,Encryptionkey string,return_UserSessionType  *UserSessionType)(Reg_Log_Type,bool){
	data := this.GetSession(userid)//.UserSessionType
	var respone_json Reg_Log_Type
	fmt.Println("userid ",userid)
	fmt.Println("session 检查",data)
	if data == nil{
		fmt.Println("session 为空")
		respone_json = Reg_Log_Type{NEED_LOGIN,INVALID,INVALID}
		return respone_json,false
	}
	mUserSessionType := data.(UserSessionType)
	if((mUserSessionType.LastDate.Unix() - time.Now().Unix()) > CLIENT_VALID_SECOND){
		respone_json = Reg_Log_Type{NEED_LOGIN,INVALID,INVALID}
		return respone_json,false

	}
	if mUserSessionType.Encryptionkey != Encryptionkey {
		respone_json = Reg_Log_Type{NEED_LOGIN,INVALID,INVALID}
		return respone_json,false
	}
	return_UserSessionType = &mUserSessionType
	return respone_json,true

}

func RegisterTypeValid(json_data RegisterType){
	valid := validation.Validation{}
	valid.Required(json_data.Tel, "Tel")
	valid.Required(json_data.UserPw, "UserPw")


	if valid.HasErrors() {
		// 如果有错误信息，证明验证没通过
		// 打印错误信息
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
			fmt.Printf("reeee")
		}
	}

}


func (this *UserGroup) Register() {
	var u RegisterType
	var respone_json Reg_Log_Type
	this.ParseForm(&u)
	fmt.Println("u:",u)
	_ ,err := m.GetUserByTel(u.Tel)
	if err == nil{
		respone_json = Reg_Log_Type{EXIST_NAME,INVALID,INVALID}
		fmt.Println("用户已经存在")
	}else {
		user := m.User{Tel:u.Tel, UserPw:u.UserPw}
		_, err = m.AddUser(&user)
		fmt.Println("user:", user)
		if err != nil {
			fmt.Println("err",err)
			respone_json = Reg_Log_Type{OPERATION_ERROR, user.Id, INVALID}
		}else{
			respone_json = Reg_Log_Type{SUCCESS, user.Id, INVALID}
		}


	}
	this.Data["json"] = &respone_json
	this.ServeJson();
}

func (this *UserGroup) Login() {
	var mLoginType LoginType
	var respone_json Reg_Log_Type

	this.ParseForm(&mLoginType)
	fmt.Println("mLoginType",mLoginType)
	user ,err:= m.GetUserByTel(mLoginType.Tel)
	if err != nil{
		fmt.Println("err",err)
		respone_json = Reg_Log_Type{INVALID_NAME,INVALID,INVALID}
	}else {
		fmt.Println("user.UserPw:", user)
		fmt.Println("Strtomd5(mLoginType.UserPw):", Strtomd5(mLoginType.UserPw))
		if user.UserPw != Strtomd5(mLoginType.UserPw) {
			respone_json = Reg_Log_Type{INVALID_PW, INVALID, INVALID}
		}else {
			//AddUserSesssion(int(user).Id,"")  //添加session
			fmt.Println("strconv.FormatInt(user.Id,10)",strconv.FormatInt(user.Id,10))
			this.AddUserSesssion(strconv.FormatInt(user.Id,10),"")
			respone_json = Reg_Log_Type{SUCCESS, user.Id, INVALID}
		}
	}

	this.Data["json"] = &respone_json
	this.ServeJson();
	return
}

func (this *UserGroup) GetSyncDataList() {


	var syncdata SynchroData

	var resp RespSynchroData
	this.ParseForm(&syncdata)
	fmt.Println("syncdata",syncdata)
	var session UserSessionType
	if data,ok := this.CheckSession(strconv.FormatInt(syncdata.UserID,10),"",&session);ok == false{
		this.Data["json"] = &data
		this.ServeJson();
		return
	}
	//var resp_data RespSynchroData
	//fmt.Println("port",this.Ctx.Input.Port())
	//userid := fmt.Sprintf("%d", syncdata.UserID)

	files,_ :=m.GetFileList(syncdata.UserID)
	fmt.Println("files",files)
	resp.RespCode = 0
	resp.Data = files
	this.Data["json"] = &resp
	this.ServeJson();
}

func (this *UserGroup) GetData(){

	var down_type DownloadType
	err := this.ParseForm(&down_type)
	if err != nil{
		fmt.Println("err",err)
	}
//	id:=this.Ctx.Input.Param(":id")
	fmt.Println("down_type",down_type)
//	int_id ,_:= strconv.Atoi(id)
	var session UserSessionType
	if data,ok := this.CheckSession(strconv.FormatInt(down_type.UserID,10),"",&session);ok == false{
		this.Data["json"] = &data
		this.ServeJson();
		return
	}
	file ,_ := m.GetFileById(down_type.Id)
	fmt.Println("file len",len(file.FileData))
	fmt.Println("file",file)
	this.Data["json"] = &file
	this.ServeJson();
	//this.Ctx.Output.Download (data_url+id+"/data.zip")
}
func (this *UserGroup) UploadData(){
	id:=this.Ctx.Input.Param(":id")
	upload_data := this.GetString("UploadData")
	var session UserSessionType
	fmt.Println("upload user id",id)
	if data,ok := this.CheckSession(id,"",&session);ok == false{
		this.Data["json"] = &data
		this.ServeJson();
		return
	}
	path ,err := m.AddFilePath(id,"mysql",upload_data)
	resp_data := RespSynchroData{SUCCESS,make([]m.FileManager,1)}
	if err != nil{
		fmt.Println("err",err)
		resp_data = RespSynchroData{OPERATION_ERROR,make([]m.FileManager,1)}//ResponeType{OPERATION_ERROR, INVALIDS,0}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}

	fmt.Println("path",path)
	fmt.Println("resp_data",resp_data)


	//this.SaveToFile("Data",path)
	this.Data["json"] = &resp_data
	this.ServeJson();
	return
}
//func (c *UserGroup) SaveToFile(file File, path string) error {
//
//	if err != nil {
//		return err
//	}
//	fmt.Println("here")
//	defer file.Close()
//	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
//	if err != nil {
////		return err
//		fmt.Println("here2")
//		os.MkdirAll(data_url+id+"/",0777)
//		f, err = os.OpenFile(data_url+id+data_file_name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
//		if err != nil{
//			//fmt.Println("err",err)
//			return err
//		}
//	}
//	defer f.Close()
//	io.Copy(f, file)
//	return nil
//}

//func (c *UserGroup) SaveToFile2(fromfile string, mShareRemote *m.ShareRemote) error {
//	file, _, err := c.Ctx.Request.FormFile(fromfile)
//	if err != nil {
//		return err
//	}
//	fmt.Println("here")
//	defer file.Close()
//	//	terr :=os.MkdirAll("/Users/xujianhua/Documents/"+id,0666)
//	//	if terr != nil{
//	//		fmt.Println("MkdirAll")
//	//	}
//	save_url := data_url+"/"+mShareRemote.Factory+"/"+mShareRemote.DeviceType+"/"+mShareRemote.DeviceModel
//	f, err := os.OpenFile(save_url+"/"+data_file_name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
//	if err != nil {
//		//		return err
//		fmt.Println("here2")
//		os.MkdirAll(save_url+"/",0777)
//		f, err = os.OpenFile(save_url+"/"+data_file_name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
//		if err != nil{
//			//fmt.Println("err",err)
//			return err
//		}
//	}
//	mShareRemote.SaveUrl = save_url+"/"+data_file_name
//	defer f.Close()
//	io.Copy(f, file)
//	return nil
//}

func (this *UserGroup)UpdateRemoteData(){
//	type UpdateRemoteDataResp struct {
//		RespCode		int
//		Data		[]m.RemoteWareHouse
//		Num				int
//	}
//	var mUpdateRemoteDataResp UpdateRemoteDataResp
//	//fmt.Println("time.Now().Unix()",time.Now().Unix(),"time",time.Unix(time.Now().Unix(),0))
//	var mUpdateRemoteDataType UpdateRemoteDataType
//	err := this.ParseForm(&mUpdateRemoteDataType)
//
//	remote_list,num,err := m.GetUpdateTime(time.Unix(mUpdateRemoteDataType.LastTime,0))
//	if err != nil{
//		mUpdateRemoteDataResp.RespCode = 2
//		mUpdateRemoteDataResp.Num = 0
//		this.Data["json"] = &mUpdateRemoteDataResp
//		this.ServeJson();
//		return
//	}
//	mUpdateRemoteDataResp.Num = num
//	mUpdateRemoteDataResp.Data = remote_list
//	this.Data["json"] = &mUpdateRemoteDataResp
//	this.ServeJson();
//	return
//	id  := this.Ctx.Input.Param(":id")
//	data,err := m.UserGetNewRemoteData(id)
//	if err != nil{
//
//	}
//	id := this.GetString("UserID")
//	var session UserSessionType
//	this.CheckSession(id,"",&session)
	data ,_:= m.GetOfficialRemoteList()
	this.Data["json"] = &data
	this.ServeJson();
	return
}

func (this *UserGroup) ChangePw() {

}



func (this *UserGroup) CheckVerifyCodeType(){
	var check_form CheckVerifyCodeType
	err_parse := this.ParseForm(&check_form)
	fmt.Println("check_form",check_form)
	if(err_parse != nil){
		resp_data := CheckVerifyCodeRespType{PARA_ERROR, check_form.Tel,INVALIDS}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}else{
		data := this.GetSession(check_form.Tel)
		fmt.Println("session data",data)

		if data == nil{
			resp_data := CheckVerifyCodeRespType{VERIFY_CODE_ERR, check_form.Tel,INVALIDS}
			this.Data["json"] = &resp_data
			this.ServeJson();
			return
		}
		userSession := data.(UserSessionType)
		if userSession.VefifyCode == check_form.VerifyCode{
				resp_data := CheckVerifyCodeRespType{SUCCESS, check_form.Tel,INVALIDS}
				this.Data["json"] = &resp_data
				this.ServeJson();
			return

		}else{
			resp_data := CheckVerifyCodeRespType{VERIFY_CODE_ERR, check_form.Tel,INVALIDS}
			this.Data["json"] = &resp_data
			this.ServeJson();
			return
		}
	}
}
//用户获取验证码
func (this *UserGroup) GetVerifyCode() {
	var resp_data CheckVerifyCodeRespType
	var userSession UserSessionType
	tel := this.GetString("Tel")
//	id_int,err:= this.GetInt("UserID")
//	if err != nil{
//		resp_data = ResponeType{PARA_ERROR, INVALIDS,0}
//		this.Data["json"] = &resp_data
//		this.ServeJson();
//		return
//	}
//	_ ,err2 := m.GetUserByTel(tel)
//	fmt.Println("err2",err2)
//	if err2 == nil{
//		resp_data = CheckVerifyCodeRespType{EXIST_NAME, tel,INVALIDS}
//		this.Data["json"] = &resp_data
//		this.ServeJson();
//		return
//	}
	data := this.GetSession(tel)
	fmt.Println("data",data)
	//
	if data == nil{
		random_Code := RandInt(100000,999999)
		random_Codes := strconv.FormatInt(int64(random_Code),10)
		userSession.LastDate = time.Now()
		userSession.Count = 1
		userSession.VefifyCode = random_Code
		//SetSelfSession("user"+id, userSession)
		this.SetSession(tel, userSession)
		//id := this.GetString("UserID")
		//VerificationCode("您的验证码是：2436。请不要把验证码泄露给其他人。",tel)
		msg := "您的验证码是："+random_Codes+"。请不要把验证码泄露给其他人。"//"您的验证码是"+random_Codes+",请不要把验证码泄露给其他人。"
		VerificationCode(msg,tel)
		resp_data = CheckVerifyCodeRespType{SUCCESS, tel,INVALIDS}
		this.Data["json"] = &resp_data
		this.ServeJson();

		return
	}else {
		userSession = data.(UserSessionType)
		mY, mM, mD := userSession.LastDate.Date()
		y, m, d := time.Now().Date()
		if (mD == d) {
			if (mM == m) {
				if (mY == y) {
					if userSession.Count > DEFAULT_MAX_MSG {
						resp_data = CheckVerifyCodeRespType{MAX_MSG, MAX_MSG_MSG,INVALIDS}
						this.Data["json"] = &resp_data
						this.ServeJson();
						return
					}else {
						random_Code := RandInt(100000, 999999)
						userSession.Count += 1
						userSession.VefifyCode = random_Code
						fmt.Println("userSession", userSession)
						//SetSelfSession("user"+id, userSession)
						this.SetSession(tel, userSession)
						//fmt.Println("SESSION",GetSelfSession("tel"+tel))
						//VerificationCode("您的验证码是：2436。请不要把验证码泄露给其他人。",tel)
						random_Codes := strconv.FormatInt(int64(random_Code),10)
						msg := "您的验证码是："+random_Codes+"。请不要把验证码泄露给其他人。"//"您的验证码是"+random_Codes+",请不要把验证码泄露给其他人。"
						VerificationCode(msg,tel)
						resp_data = CheckVerifyCodeRespType{SUCCESS, tel,INVALIDS}
						this.Data["json"] = &resp_data
						this.ServeJson();
						return
					}
				}
			}
		}
	}

	resp_data = CheckVerifyCodeRespType{PARA_ERROR, tel,INVALIDS}
	this.Data["json"] = &resp_data
	this.ServeJson();
	return
}
//更新用户数据
func (this *UserGroup) UpdateUser() {
	//var userSession UserSessionType
	var update MobileUpdateUserType
	var resp_data ResponeType

	err := 	this.ParseForm(&update)

	if err != nil{
		resp_data = ResponeType{PARA_ERROR, INVALIDS,0}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}

	if update.UserPw == ""{
		resp_data = ResponeType{PARA_ERROR, INVALIDS,0}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
	if update.Tel == ""{
		resp_data = ResponeType{PARA_ERROR, INVALIDS,0}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
	err1 := m.UpdateUserByTel(update.Tel,update.UserPw)
//	//data := GetSelfSession("user"+ids)
//	data := this.GetSession(ids)
//	fmt.Println("data",data)
//	//
//	if data == nil{
//		resp_data = ResponeType{VERIFY_CODE_ERR, INVALIDS,0}
//		this.Data["json"] = &resp_data
//		this.ServeJson();
//		return
//	}else {
//		userSession = data.(UserSessionType)
//		if (userSession.VefifyCode == int(Code)){
//			err2:= m.UpdateUserpw(int64(id),pw)
//			if err2 != nil{
//				fmt.Println("err2",err2)
//				resp_data = ResponeType{OPERATION_ERROR, INVALIDS,0}
//				this.Data["json"] = &resp_data
//				this.ServeJson();
//				return
//			}
//			resp_data = ResponeType{SUCCESS, INVALIDS,0}
//			this.Data["json"] = &resp_data
//			this.ServeJson();
//			return
//		}
//	}
	if err1 != nil{
		fmt.Println("err1",err1)
		resp_data = ResponeType{OPERATION_ERROR, INVALIDS,0}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
	resp_data = ResponeType{SUCCESS, INVALIDS,0}
	this.Data["json"] = &resp_data
	this.ServeJson();
	return
}
//用户分享遥控器数据
func (this *UserGroup) ShareRemote(){

//	Factory := this.Ctx.Input.Param(":Factory")
//	DeviceModel := this.Ctx.Input.Param(":DeviceModel")
//	DeviceType := this.Ctx.Input.Param(":DeviceType")
//	Descript := this.Ctx.Input.Param(":Descript")
//	id := this.Ctx.Input.Param(":id")

	//session


	type TMP struct {
		Id				int64
		StorageCode   string
		Factory			string
		DeviceModel			string
		DeviceType			string
		Descript			string

	}
	var tmp_data TMP
	var data m.ShareRemoteWareHouse
	//err_parse := json.Unmarshal(this.Ctx.Input.RequestBody, &tmp_data)
	err_parse := this.ParseForm(&tmp_data)

	fmt.Println("tmp_data",tmp_data)
	if(err_parse != nil){
		fmt.Println("err_parse",err_parse)
		resp_data := ResponeType{PARA_ERROR, INVALIDS,0}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
	fmt.Println("strconv.FormatInt(tmp_data.Id,10)",strconv.FormatInt(tmp_data.Id,10))
	var session UserSessionType
	if data,ok := this.CheckSession(strconv.FormatInt(tmp_data.Id,10),"",&session);ok == false{
		this.Data["json"] = &data
		this.ServeJson();
		return
	}


	data.StorageCode = tmp_data.StorageCode
	mShareRemote := m.ShareRemote{Factory:tmp_data.Factory,DeviceModel:tmp_data.DeviceModel,DeviceType:tmp_data.DeviceType,Descript:tmp_data.Descript}
	fmt.Println("分享的数据",data)
//			int64_id,err1 := tmp_data.id//strconv.ParseInt(id,10,64)
//			fmt.Println("err1",err1)
//			if err1 == nil{
				err2 := m.AddShareRemote(&mShareRemote,tmp_data.Id,data)
				fmt.Println("err2",err2)
				if err2 == nil{
					resp_data := ResponeType{SUCCESS, INVALIDS,0}
					this.Data["json"] = &resp_data
					this.ServeJson();
					return
				}
//			}

//	err := this.SaveToFile2("testData",&mShareRemote)
//	fmt.Println("err",err)
//	if err == nil{
//		int64_id,err1 := strconv.ParseInt(id,10,64)
//		fmt.Println("err1",err1)
//		if err1 == nil{
//			err2 := m.AddShareRemote(&mShareRemote,int64_id)
//			fmt.Println("err2",err2)
//			if err2 == nil{
//				resp_data := ResponeType{SUCCESS, INVALIDS,0}
//				this.Data["json"] = &resp_data
//				this.ServeJson();
//				return
//			}
//		}

//	}
	resp_data := ResponeType{SUCCESS, INVALIDS,0}
	this.Data["json"] = &resp_data
	this.ServeJson();
	return
}
//下载用户数据
func (this *UserGroup) DownLoadShareRemote() {
	//var id BsonId
	//err:= this.ParseForm(&id)
	UserId:= this.GetString("UserID")
	ObjectId := this.GetString("ObjectId")
	var resp_data m.DownSearchDataType

	fmt.Println("ObjectId",ObjectId)
	fmt.Println("UserId",UserId)

	if ObjectId == ""{
		resp_data.RespCode = PARA_ERROR
		this.Data["json"] = &resp_data

		this.ServeJson();
		return
	}
	//session
	var session UserSessionType
	if data,ok := this.CheckSession(UserId,"",&session);ok == false{
		this.Data["json"] = &data
		this.ServeJson();
		return
	}
//	if err != nil{
//		//this.Data["json"] = &
//		this.ServeJson();
//		return
//	}
	//fmt.Println("ObjetcId",id)
	data,err1:=  m.GetShareRemote(ObjectId)
	if err1 != nil{
		fmt.Println("err1",err1)
		resp_data = m.DownSearchDataType{OPERATION_ERROR,data}
		this.Data["json"] = &resp_data

		this.ServeJson();
		return
	}
	resp_data = m.DownSearchDataType{SUCCESS,data}

	this.Data["json"] = &resp_data
	this.ServeJson();
	return
}
//搜索用户分享的数据
func (this *UserGroup) SearcShareRemote(){
	//id := this.GetString("UserID")
	//session
//	var session UserSessionType
//	if data,ok := this.CheckSession(id,"",&session);ok == false{
//		this.Data["json"] = &data
//		this.ServeJson();
//		return
//	}
	var resp_data m.RespSearchType
	SearchText :=this.GetString("SearchText")
	fmt.Println("SearchText",SearchText)
	var mShareRemote []m.ShareRemote
	var num int64
	var err error
	mShareRemote ,num,err = m.SearchShareRemote(SearchText)
	if err != nil{
		fmt.Println("mShareRemote",mShareRemote,num,err)
		resp_data = m.RespSearchType{OPERATION_ERROR,mShareRemote}//	resp_data := ResponeType{SUCCESS, INVALIDS,0}
		this.Data["json"] = &resp_data
		this.ServeJson();
		return
	}
	fmt.Println("mShareRemote",mShareRemote,num,err)
	resp_data = m.RespSearchType{SUCCESS,mShareRemote}//	resp_data := ResponeType{SUCCESS, INVALIDS,0}
	this.Data["json"] = &resp_data
	this.ServeJson();
	return
}

//获取手机没有而服务器有的空调遥控数据
//传入的为空调开同或者关机的数据
func (this *UserGroup) GetMatchRemoteData() {
	//boot := "4528,4364,643,1549,641,456,638,1554,641,1548,644,453,636,457,641,1550,641,454,641,453,645,1546,642,456,637,458,641,1549,644,1546,642,454,640,1551,641,454,640,1550,644,454,639,1549,642,1551,639,1550,672,1520,641,1549,645,1547,641,455,636,1555,642,454,639,456,641,453,643,453,642,453,641,1549,644,1548,638,456,641,455,645,452,635,459,642,451,642,453,643,452,643,453,640,1549,670,1522,644,1546,644,1548,642,1548,641,1551,638,5203,4553,4368,644,1547,642,456,636,1551,642,1551,643,450,645,452,640,1549,642,454,644,450,644,1548,644,452,641,453,642,1549,642,1550,639,457,641,1547,644,452,642,1549,641,456,640,1551,641,1548,670,1521,645,1546,641,1549,642,1548,643,454,639,1549,642,455,640,456,639,456,639,455,640,455,640,1551,615,1579,640,454,640,456,642,454,640,454,642,455,639,455,641,454,642,454,640,1549,642,1549,645,1549,641,1549,642,1547,642,1548,642"
	boot := this.GetString("BootCode")
//	flow,err := BootAnalyse(boot)
//	if err != nil{
//		fmt.Println("err",err)
//		return
//	}
	result,num,err := m.MatchOfficialAcRemote(boot)
	fmt.Println("num",num,"err",err)
//	type test struct {
//		Boot					string
//	}
//	result := test {Boot:flow}
	this.Data["json"] = &result
	this.ServeJson();
	return
}

//获取手机没有而服务器有的空调遥控数据
//传入的为空调开同或者关机的数据
func (this *UserGroup) GetMatchRemoteDataByType() {
	//boot := "4528,4364,643,1549,641,456,638,1554,641,1548,644,453,636,457,641,1550,641,454,641,453,645,1546,642,456,637,458,641,1549,644,1546,642,454,640,1551,641,454,640,1550,644,454,639,1549,642,1551,639,1550,672,1520,641,1549,645,1547,641,455,636,1555,642,454,639,456,641,453,643,453,642,453,641,1549,644,1548,638,456,641,455,645,452,635,459,642,451,642,453,643,452,643,453,640,1549,670,1522,644,1546,644,1548,642,1548,641,1551,638,5203,4553,4368,644,1547,642,456,636,1551,642,1551,643,450,645,452,640,1549,642,454,644,450,644,1548,644,452,641,453,642,1549,642,1550,639,457,641,1547,644,452,642,1549,641,456,640,1551,641,1548,670,1521,645,1546,641,1549,642,1548,643,454,639,1549,642,455,640,456,639,456,639,455,640,455,640,1551,615,1579,640,454,640,456,642,454,640,454,642,455,639,455,641,454,642,454,640,1549,642,1549,645,1549,641,1549,642,1547,642,1548,642"
	boot := this.GetString("BootCode")
	//	flow,err := BootAnalyse(boot)
	//	if err != nil{
	//		fmt.Println("err",err)
	//		return
	//	}
	result,num,err := m.MatchOfficialAcRemote(boot)
	fmt.Println("num",num,"err",err)
	//	type test struct {
	//		Boot					string
	//	}
	//	result := test {Boot:flow}
	this.Data["json"] = &result
	this.ServeJson();
	return
}

//检测app的版本
func  (this *UserGroup)GetLastVersionApp(){
	version := this.GetString("Version")
	mNewVersionApp ,count,err := m.GetLastVersion()
	var resp LastVersionType
	if err != nil{
		resp.RespCode = OPERATION_ERROR
		this.Data["json"] = &resp
		this.ServeJson();
		return
	}
	if count == 0{
		resp.RespCode = NO_NEW_VERSION
		this.Data["json"] = &resp
		this.ServeJson();
		return
	}
	if version == mNewVersionApp.Version{
		resp.RespCode = NO_NEW_VERSION
		this.Data["json"] = &resp
		this.ServeJson();
		return
	}
	mNewVersionApp.URL = down_app_url+strconv.FormatInt(int64(mNewVersionApp.Id),10)//"http://localhost:8080/api/download_app/"+strconv.FormatInt(int64(mNewVersionApp.Id),10)
	resp.RespCode = SUCCESS
	resp.Data = mNewVersionApp
	this.Data["json"] = &resp
	this.ServeJson();
	return
}
//下载最新的app
func  (this *UserGroup)GetDownLoadAPP(){
	//id  := this.Ctx.Input.Param(":id")
	mNewVersionApp ,count,_ := m.GetLastVersion()
	if count >= 0{
		fmt.Println("mNewVersionApp.URL",mNewVersionApp.URL)
		this.Ctx.Output.Download (mNewVersionApp.URL)
	}else{

	}
}

func (this *UserGroup) TestRemoteData(){
	data ,_:= m.GetOfficialRemoteList()
	this.Data["json"] = &data
	this.ServeJson();
	return
}

func (this *UserGroup) Test(){
	data := DeviceScript{}
	data.Status = make([]string,3)
	data.Status[0]  ="sssshsh"
	fmt.Println("data",data)
	resp_data := ResponeType{SUCCESS, INVALIDS,0}
	this.Data["json"] = &resp_data
	this.ServeJson()
	return
}

func (this *UserGroup) TestFloat(){
////	var size_f float64
////	size_f = float64(l1000)/float64(10)
	size := 15*1000/1024

	fmt.Println("size",strconv.Itoa(size/1000)+"."+strconv.Itoa(size%1000))
	resp_data := ResponeType{SUCCESS, INVALIDS,0}
	this.Data["json"] = &resp_data
	this.ServeJson()
	return
}


